package com.hh.game.common.base.exception;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;

import java.util.concurrent.ConcurrentHashMap;

public class GameException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    /**
     * 请求重复
     */
    public static final short REQUEST_REPEAT = 400;
    /**
     * 非法请求
     */
    public static final short ILLEGAL_REQUEST = 401;
    /**
     * 服务器异常
     */
    public static final short SERVER_ERROR = 500;
    /**
     * 服务器工作线程处理队列已满
     */
    public static final short SERVER_WORK_POOL_FULL = 501;
    /**
     * 服务器连接已满
     */
    public static final short SERVER_CONNECTION_FULL = 502;
    /**
     * 服务器数据异常
     */
    public static final short SERVER_DATA_ERROR = 503;

    private short id;
    private String route;

    public GameException(short id, String message) {
        super(message);
        this.id = id;
    }


    public GameException(short id, String route, String message) {
        super(message);
        this.id = id;
        this.route = route;
    }


    public GameException(short id, String route, String message, Throwable cause) {
        super(message, cause);
        this.id = id;
        this.route = route;
    }

    public void setRoute(String route) {
        this.route = route;
    }

    public JSONObject getClientResult() {
        JSONObject result = new JSONObject();
        result.put("route", this.route);
        result.put("eid", this.id);
        result.put("msg", this.getMessage());
        return result;
    }


    @Override
    public String getMessage() {
        return String.format("GameExceptionId : %d , route : %s ,message : %s ", id, route, super.getMessage());
    }

    /**
     * {@link GameException#getMessage()}
     *
     * @param throwableMessage
     * @return
     */
    public static GameException parse(String throwableMessage) {
        int index;
        if (throwableMessage == null || throwableMessage.indexOf(GameException.class.getName()) == -1
                || (index = throwableMessage.indexOf("GameExceptionId")) < 0) {
            return null;
        }
        String tmp = throwableMessage.substring(index);
        String[] args = tmp.split(",");
        if (args.length < 3) {
            return null;
        }

        String[] arr = args[0].split(":");
        if (!checkArrayLength(arr, 2)) {
            return null;
        }
        short id = Short.parseShort(StrUtil.trim(arr[1]));

        arr = args[1].split(":");
        if (!checkArrayLength(arr, 2)) {
            return null;
        }
        String route = StrUtil.trim(arr[1]);
        route = StrUtil.isNullOrUndefined(route) ? null : route;

        int indexOf = args[2].indexOf(":");
        if (indexOf < 0) {
            return null;
        }
        String msg = StrUtil.trim(args[2].substring(indexOf + 1));
        if (!StrUtil.isNullOrUndefined(msg)) {
            if (args.length > 3) {
                for (int i = 3; i < args.length; i++) {
                    msg += args[i];
                }
            }
        } else {
            msg = null;
        }

        GameException ex = new GameException(id, route, msg);
        return ex;
    }


    private static boolean checkArrayLength(String[] arr, int len) {
        return arr != null && arr.length == len;
    }

    @Override
    public synchronized Throwable fillInStackTrace() {
        return null;
    }
}
